opendir(DH,"/Users/yaping/Documents/workspace/result/ASM/") or die;
my @normal_unjusted;
my @tumor_unjusted;
my @normal_adjusted;
my @tumor_adjusted;
foreach my $file(readdir(DH)){
	if($file=~/methylCGsRich_ASM_AllSnp_(\w+)Merge_(\w+)_pValue.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file") or die;
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @out;
		my $count =0;
		my $total_count = 0;
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			$total_count++;
			if($splitin[2] < 0.001){
				my $start=$splitin[1];
				my $end=$splitin[1]+1;
				$count++;
				push(@out,"$chr\t$sample\texon\t$start\t$end\t0\t\+\t.\n");
				if($sample eq "normal"){
					push(@normal_unjusted,"$chr\t$sample\tSNP\t$start\t$end\t0\t\+\t.\n");
				}
				else{
					push(@tumor_unjusted,"$chr\t$sample\tSNP\t$start\t$end\t0\t\+\t.\n");
				}
			}
		
		}
		print("$sample\t$chr\t$count\t$total_count\n");
		my $out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_sigGroup0.001.gtf";
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$out_file");
		print OUT @out;
		close(OUT);
		
	}
	if($file=~/methylCGsRich_ASM_AllSnp_(\w+)Merge_(\w+)_pValue_adjusted.txt/){
		my $sample=$1;
		my $chr=$2;
		open(FH,"</Users/yaping/Documents/workspace/result/ASM/$file") or die;
		my @in=<FH>;
		chomp(@in);
		close(FH);
		my @out;
		my $count =0;
		my $total_count = 0;
		foreach my $line(@in){
			my @splitin = split "\t",$line;
			$total_count++;
			if($splitin[4] < 0.001){
				my $start=$splitin[1];
				my $end=$splitin[1]+1;
				$count++;
				push(@out,"$chr\t$sample\texon\t$start\t$end\t0\t\+\t.\n");
				if($sample eq "normal"){
					push(@normal_adjusted,"$chr\t$sample\tSNP\t$start\t$end\t0\t\+\t.\n");
				}
				else{
					push(@tumor_adjusted,"$chr\t$sample\tSNP\t$start\t$end\t0\t\+\t.\n");
				}
			}
		
		}
		print("$sample\t$chr\t$count\t$total_count\n");
		my $out_file="methylCGsRich_ASM_AllSnp_".$sample."Merge_".$chr."_sigGroup0.001_adjustedP.gtf";
		open(OUT,">/Users/yaping/Documents/workspace/result/ASM/$out_file");
		print OUT @out;
		close(OUT);
		
	}
}

open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_sigGroup0.001_unjustedP.gtf");
print OUT @normal_unjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_sigGroup0.001_unjustedP.gtf");
print OUT @tumor_unjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_normalMerge_sigGroup0.001_adjustedP.gtf");
print OUT @normal_adjusted;
close(OUT);
open(OUT,">/Users/yaping/Documents/workspace/result/ASM/methylCGsRich_ASM_AllSnp_tumorMerge_sigGroup0.001_adjustedP.gtf");
print OUT @tumor_adjusted;
close(OUT);